#include"sysif.h"
#include<stdlib.h>
#include<pthread.h>
#include"recvF.h"

FILE* prepareF(char* filename, long offset)
{
	FILE* tmp = fopen(filename, "wb");
	fseek(tmp, offset, SEEK_SET);
	return tmp;
}

void* threadProxy(void* __arg)
{
	pThreadArg arg= (pThreadArg)__arg;
	pTCB cmd = &(arg->cmd);
	FILE* fout = prepareF(cmd->fileName, cmd->offset);
	getFileSeg(arg->sock, fout, cmd);

	fclose(fout);
	free(arg);
	printf("Thread reading from %ld for %dByte complete\n", cmd->offset, cmd->dataLen);

	return NULL;
}

pthread_t crtDownThread(pThreadArg arg)
{
	pthread_t tid;

	pthread_create(&tid, NULL, threadProxy, (void*)arg);
	return tid;
}
